package webModule.product
{
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	import com.touchX.display.LoaderManager;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.parsers.Collada;

	public class ProductModule extends EventDispatcher
	{
		private var _loader:QueueLoader;
		
		private var _productName:String;
		private var _thumbnailUrl:String;
		private var _moduleUrl:String;
		private var _moduleMapUrl:String;
		private var _mapName:String;
		private var _moduleScale:Number;
		private var _description:String;
		
		private var _thumbnail:Bitmap;
		private var _module:Collada;
		private var _container:DisplayObjectContainer;
		private var _view:ProductView;
		private var _map:Bitmap;
		
		public function get productName():String{return _productName;}
		public function get thumbnail():Bitmap{ return _thumbnail;}
		public function get module():Collada{return _module;}
		public function get view():ProductView{return _view};
		
		public function ProductModule(dataXml:XML,viwerContainer:DisplayObjectContainer)
		{
			_loader=LoaderManager.getInstance().loader;
			_container=viwerContainer;
			
			_productName=dataXml.productName;
			_thumbnailUrl=dataXml.thumbnail;
			_moduleUrl=dataXml.moduleUrl;
			_moduleMapUrl=dataXml.mapUrl;
			_mapName=dataXml.mapName;
			_moduleScale=Number(dataXml.scale);
			_description=dataXml.description;
			
			
			this.addEventListener(MouseEvent.CLICK,loadModule);
			
			_loader.addItem(_thumbnailUrl,null,{title:_thumbnailUrl,target:this});
			if(!_loader.isLoading()) _loader.execute();
		}
		private function loadModule(evt:MouseEvent):void{
			LoaderManager.getInstance().createLoader();
			_loader=LoaderManager.getInstance().loader;
			_loader.addItemAt(0,this._moduleMapUrl,null,{title:"matMaterial",target:this});
			_loader.addItem(_moduleUrl,null,{title:"XML:module",target:this});
			if(!_loader.isLoading()) _loader.execute();
		}
		
		public function onItemComplete(evt:QueueLoaderEvent):void{
			if(evt.title=="matMaterial"){
				_map=_loader.getItemByTitle(evt.title).content as Bitmap;
			}else if(evt.title=="XML:module"){
				var matList:MaterialsList=new MaterialsList();
				var mapMaterial:BitmapMaterial=new BitmapMaterial(_map.bitmapData);
				matList.addMaterial(mapMaterial,this._mapName);
				this._module=new Collada(_loader.getItemByTitle(evt.title).content,matList,_moduleScale);
				this.dispatchEvent(new ProductEvent(ProductEvent.SHOW_PRODUCT,this));
			}else{
				_view=new ProductView(this);
				_thumbnail=_loader.getItemByTitle(evt.title).content as Bitmap;
				_container.addChild(_view);
			}
		}
		
	}
}